import pgzrun

WIDTH = 700
HEIGHT = 700
red = Actor('red')
red.x = 25
red.y = 25
blue = Actor('blue')
blue.x = 675
blue.y = 675
up_weapons = []
down_weapons = []
left_weapons = []
right_weapons = []
flag = 0


def draw():
    global flag
    screen.clear()
    if flag == 0:
        red.draw()
        blue.draw()
        for w in up_weapons:
            w.draw()
        for w in down_weapons:
            w.draw()
        for w in left_weapons:
            w.draw()
        for w in right_weapons:
            w.draw()
    elif flag == 1:
        screen.draw.text('BLUE WINS', (250, 250), fontsize=50, color='blue')
    elif flag == 2:
        screen.draw.text('RED WINS', (250, 250), fontsize=50, color='red')
    elif flag == 3:
        screen.draw.text('DRAW', (300, 250), fontsize=50, color='purple')


def on_key_down(key):
    global flag
    if flag == 0:
        if key == keys.T:
            w = Actor('weapon2')
            w.x = red.x
            w.y = red.y - 35
            up_weapons.append(w)
        if key == keys.G:
            w = Actor('weapon2')
            w.x = red.x
            w.y = red.y + 35
            down_weapons.append(w)
        if key == keys.F:
            w = Actor('weapon1')
            w.x = red.x - 35
            w.y = red.y
            left_weapons.append(w)
        if key == keys.H:
            w = Actor('weapon1')
            w.x = red.x + 35
            w.y = red.y
            right_weapons.append(w)
        if key == keys.I:
            w = Actor('weapon2')
            w.x = blue.x
            w.y = blue.y - 35
            up_weapons.append(w)
        if key == keys.K:
            w = Actor('weapon2')
            w.x = blue.x
            w.y = blue.y + 35
            down_weapons.append(w)
        if key == keys.J:
            w = Actor('weapon1')
            w.x = blue.x - 35
            w.y = blue.y
            left_weapons.append(w)
        if key == keys.L:
            w = Actor('weapon1')
            w.x = blue.x + 35
            w.y = blue.y
            right_weapons.append(w)


def update():
    global flag
    if flag == 0:
        if red.top > 0 and keyboard.w:
            red.y -= 5
        elif red.bottom < 700 and keyboard.s:
            red.y += 5
        elif red.left > 0 and keyboard.a:
            red.x -= 5
        elif red.right < 700 and keyboard.d:
            red.x += 5
        if blue.top > 0 and keyboard.up:
            blue.y -= 5
        elif blue.bottom < 700 and keyboard.down:
            blue.y += 5
        elif blue.left > 0 and keyboard.left:
            blue.x -= 5
        elif blue.right < 700 and keyboard.right:
            blue.x += 5
        for w in up_weapons:
            w.y -= 5
            if w.y < 0:
                up_weapons.remove(w)
            if w.colliderect(red):
                flag = 1
            if w.colliderect(blue):
                flag += 2
            for i in down_weapons:
                if w.colliderect(i):
                    up_weapons.remove(w)
                    down_weapons.remove(i)
            for i in left_weapons:
                if w.colliderect(i):
                    up_weapons.remove(w)
                    left_weapons.remove(i)
            for i in right_weapons:
                if w.colliderect(i):
                    up_weapons.remove(w)
                    right_weapons.remove(i)
        for w in down_weapons:
            w.y += 5
            if w.y > 700:
                down_weapons.remove(w)
            if w.colliderect(red):
                flag = 1
            if w.colliderect(blue):
                flag += 2
            for i in left_weapons:
                if w.colliderect(i):
                    down_weapons.remove(w)
                    left_weapons.remove(i)
            for i in right_weapons:
                if w.colliderect(i):
                    down_weapons.remove(w)
                    right_weapons.remove(i)
        for w in left_weapons:
            w.x -= 5
            if w.x < 0:
                left_weapons.remove(w)
            if w.colliderect(red):
                flag = 1
            if w.colliderect(blue):
                flag += 2
            for i in right_weapons:
                if w.colliderect(i):
                    left_weapons.remove(w)
                    right_weapons.remove(i)
        for w in right_weapons:
            w.x += 5
            if w.x > 700:
                right_weapons.remove(w)
            if w.colliderect(red):
                flag = 1
            if w.colliderect(blue):
                flag += 2
        if red.colliderect(blue):
            flag = 3


pgzrun.go()
